﻿using Assets.XyzCode.Fight.Game.Enemy;
using Assets.XyzCode.Fight.Game.Player;
using Assets.XyzCode.Fight.UI;
using Assets.XyzCode.VisualEffect;

using System.Collections;

using UnityEngine;

namespace Assets.XyzCode.Fight
{
    /// <summary>
    /// 战斗场景
    /// </summary>
    public class FightScene : MonoBehaviour
    {
        /** 死亡小姐行为 */
        private Player_MissDie_Behav _playerMissDieBehav;

        /// <summary>
        /// Awake,
        /// 这个函数和 Cocos 中的 onLoad 函数功能一样
        /// </summary>
        private void Awake()
        {
            _playerMissDieBehav = GameObject.Find("/Game/Player_MissDie_")
                .AddComponent<Player_MissDie_Behav>();
        }

        /// <summary>
        /// Start
        /// </summary>
        private void Start()
        {
            StartCoroutine(Prepare_XC());
        }

        /// <summary>
        /// Update
        /// </summary>
        private void Update()
        {
            if (Input.GetKeyUp(KeyCode.L))
            {
                _playerMissDieBehav.DoFastForward();
                return;
            }

            int dirX = 0;
            int dirZ = 0;

            if (Input.GetKey(KeyCode.D))
            {
                dirX = 1;
            }
            else
            if (Input.GetKey(KeyCode.A))
            {
                dirX = -1;
            }

            if (Input.GetKey(KeyCode.W))
            {
                dirZ = 1;
            }
            else
            if (Input.GetKey(KeyCode.S))
            {
                dirZ = -1;
            }

            if (Input.GetKey(KeyCode.J))
            {
                _playerMissDieBehav.DoPunch(dirX, dirZ);
                return;
            }

            if (0 == dirX 
             && 0 == dirZ)
            {
                _playerMissDieBehav.DoStand();
            }
            else
            {
                _playerMissDieBehav.DoRun(dirX, dirZ);
            }
        }

        /// <summary>
        /// LateUpdate
        /// </summary>
        private void LateUpdate()
        {
            Vector3 cameraPos = Camera.main.transform.position;
            Vector3 playerPos = _playerMissDieBehav.transform.position;

            cameraPos = Vector3.Lerp(
                cameraPos,
                playerPos + Vector3.up * 20 + Vector3.back * 32, // 计算得到新的摄像机位置
                3.2f * Time.deltaTime // 不要一下子移过去, 而是有一个缓动
            );

            Camera.main.transform.position = cameraPos;
        }

        /// <summary>
        /// ( 携程方式 ) 准备
        /// </summary>
        /// <returns>枚举迭代器</returns>
        private IEnumerator Prepare_XC()
        {
            WaitForEndOfFrame wait = new WaitForEndOfFrame();

            yield return wait;
            HitLitFactory.Prepare();

            yield return wait;
            FloatTextFactory.Prepare();

            // 获取游戏节点
            Transform tGame = GameObject.Find("/Game").transform;

            for (int i = 0; i < 1; i++)
            {
                for (int enemyId = 1001; enemyId <= 1004; enemyId++)
                {
                    EnemyFactory.Future f = EnemyFactory.CreateByIdAsync(enemyId);
                    yield return f;

                    if (null == f 
                     || f.IsEmptyResult())
                    {
                        continue;
                    }

                    GameObject goNewEnemy = f.GetEnemyObj();
                    goNewEnemy.transform.parent = tGame;
                    // XXX 注意: 上面这一句,
                    // 这里和 Cocos 不太一样!
                    // Cocos 的做法是 tGame.addChild(goEnemy);
                    // 而在 Unity 里则是用认爹的方式...
                    // 
                    goNewEnemy.transform.position = new Vector3(
                        Random.Range(-5.0f, 5.0f),
                        0, // 站在地面上
                        Random.Range(-5.0f, 5.0f)
                    );

                    yield return wait;
                }
            }

            // XXX 注意, 主角在各个敌人之间穿梭的时候位置关系始终都是正确的.
            // 这里并不像 Cocos 那样需要修改 zIndex 数值,
            // 因为 3D 游戏中有 Z 轴这个维度,
            // 引擎在渲染的时候自然会处理这个维度...
            yield break;
        }
    }
}
